package rxjava.application.com.androidmyp.View;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.View;

import java.util.List;

/**
 * Created by Administrator on 2016/3/11.
 */
public class CircleAnimation {
    public int radius; //半径
    public int mViewSize; //控件的个数
    public List<View> mViewList;//控件数据源
    public boolean mViewIsopen;//CircleAnimation是否打开了
    private double degree; //角度
    private CircleClickListener listener;//开关时候的回调接口

    public CircleAnimation(int radius, List<View> ViewList, CircleClickListener listener) {
        this.radius = radius;
        this.mViewList = ViewList;
        this.listener = listener;
        initParams();
    }

    private void initParams() {
        this.mViewSize = mViewList.size();
        this.degree = Math.toRadians(90);
        mViewIsopen = false;
    }

    private void openCircle() {
        for (int i = 0; i < mViewSize; i++) {
            viewStart(i, mViewList.get(i));
        }
        mViewIsopen = true;
    }

    private void closeCircle() {
        for (int i = 0; i < mViewSize; i++) {
            viewEnd(i, mViewList.get(i));
        }
        mViewIsopen = false;
    }

    private void viewStart(int index, View view) {
        double currentDegree = degree / (mViewSize - 1) * index;
        float translateX = -(float) (radius * Math.sin(currentDegree));
        float traslateY = -(float) (radius * Math.cos(currentDegree));
        AnimatorSet set = new AnimatorSet();
        set.playTogether(
                ObjectAnimator.ofFloat(view, "translationX", 0f, translateX),
                ObjectAnimator.ofFloat(view, "translationY", 0f, traslateY),
                ObjectAnimator.ofFloat(view, "scaleX", 0f, 1f),
                ObjectAnimator.ofFloat(view, "scaleY", 0f, 1f),
                ObjectAnimator.ofFloat(view, "alpha", 0f, 1)
        );
        set.setDuration(1000);
        view.setVisibility(View.VISIBLE);
        set.start();
    }

    public void viewEnd(int index, View view) {
        double currentDegree = degree / (mViewSize - 1) * index;
        float translateX = -(float) (radius * Math.sin(currentDegree));
        float traslateY = -(float) (radius * Math.cos(currentDegree));
        AnimatorSet set = new AnimatorSet();
        set.playTogether(
                ObjectAnimator.ofFloat(view, "translationX", translateX, 0f),
                ObjectAnimator.ofFloat(view, "translationY", traslateY, 0f),
                ObjectAnimator.ofFloat(view, "scaleX", 1f, 0f),
                ObjectAnimator.ofFloat(view, "scaleY", 1f, 0f),
                ObjectAnimator.ofFloat(view, "alpha", 1f, 0f)
        );
        set.setDuration(1000);
        set.start();
    }

    public void start() {
        if (mViewIsopen) {
            closeCircle();
            listener.onClick(mViewIsopen);
        } else {
            openCircle();
            listener.onClick(mViewIsopen);
        }
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public List<View> getmViewList() {
        return mViewList;
    }

    public void setmViewList(List<View> mViewList) {
        this.mViewList = mViewList;
    }

    public interface CircleClickListener {
        public void onClick(boolean isOpen);
    }
}
